home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ImageMapArea.java < prev    next >
Text File  |  1998-09-15  |  11KB  |  346 lines

  1. /*
  2.  * @(#)ImageMapArea.java    1.9 98/03/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.Graphics;
  32. import java.awt.Image;
  33. import java.awt.image.*;
  34. import java.util.StringTokenizer;
  35. import java.net.URL;
  36. import java.net.MalformedURLException;
  37.  
  38. /**
  39.  * The base ImageArea class.
  40.  * This class performs the basic functions that most ImageArea
  41.  * classes will need and delegates specific actions to the subclasses.
  42.  *
  43.  * @author     Jim Graham
  44.  * @version     1.9, 03/18/98
  45.  */
  46. class ImageMapArea implements ImageObserver {
  47.     /** The applet parent that contains this ImageArea. */
  48.     ImageMap parent;
  49.     /** The X location of the area (if rectangular). */
  50.     int X;
  51.     /** The Y location of the area (if rectangular). */
  52.     int Y;
  53.     /** The size().width of the area (if rectangular). */
  54.     int W;
  55.     /** The size().height of the area (if rectangular). */
  56.     int H;
  57.     /**
  58.      * This flag indicates whether the user was in this area during the
  59.      * last scan of mouse locations.
  60.      */
  61.     boolean entered = false;
  62.     /** This flag indicates whether the area is currently highlighted. */
  63.     boolean active = false;
  64.  
  65.     /**
  66.      * This is the default highlight image if no special effects are
  67.      * needed to draw the highlighted image.  It is created by the
  68.      * default "makeImages()" method.
  69.      */
  70.     Image hlImage;
  71.  
  72.     /**
  73.      * This is the status string requested by this area.  Only the
  74.      * status string from the topmost area which has requested one
  75.      * will be displayed.
  76.      */
  77.     String status;
  78.  
  79.     /**
  80.      * Initialize this ImageArea as called from the applet.
  81.      * If the subclass does not override this initializer, then it
  82.      * will perform the basic functions of setting the parent applet
  83.      * and parsing out 4 numbers from the argument string which specify
  84.      * a rectangular region for the ImageArea to act on.
  85.      * The remainder of the argument string is passed to the handleArg()
  86.      * method for more specific handling by the subclass.
  87.      */
  88.     public void init(ImageMap parent, String args) {
  89.     this.parent = parent;
  90.     StringTokenizer st = new StringTokenizer(args, ", ");
  91.     X = Integer.parseInt(st.nextToken());
  92.     Y = Integer.parseInt(st.nextToken());
  93.     W = Integer.parseInt(st.nextToken());
  94.     H = Integer.parseInt(st.nextToken());
  95.     if (st.hasMoreTokens()) {
  96.         // hasMoreTokens() Skips the trailing comma
  97.         handleArg(st.nextToken(""));
  98.     } else {
  99.         handleArg(null);
  100.     }
  101.     makeImages();
  102.     }
  103.  
  104.     /**
  105.      * This method handles the remainder of the argument string after
  106.      * the standard initializer has parsed off the 4 rectangular
  107.      * parameters.  If the subclass does not override this method,
  108.      * the remainder will be ignored.
  109.      */
  110.     public void handleArg(String s) {
  111.     }
  112.  
  113.     /**
  114.      * This method loads any additional media that the ImageMapArea
  115.      * may need for its animations.
  116.      */
  117.     public void getMedia() {
  118.     }
  119.  
  120.     /**
  121.      * This method is called every animation cycle if there are any
  122.      * active animating areas.
  123.      * @return true if this area requires further animation notifications
  124.      */
  125.     public boolean animate() {
  126.     return false;
  127.     }
  128.  
  129.     /**
  130.      * This method sets the image to be used to render the ImageArea
  131.      * when it is highlighted.
  132.      */
  133.     public void setHighlight(Image img) {
  134.     hlImage = img;
  135.     }
  136.  
  137.     /**
  138.      * This method handles the construction of the various images
  139.      * used to highlight this particular ImageArea when the user
  140.      * interacts with it.
  141.      */
  142.     public void makeImages() {
  143.     setHighlight(parent.getHighlight(X, Y, W, H));
  144.     }
  145.  
  146.     /**
  147.      * The repaint method causes the area to be repainted at the next
  148.      * opportunity.
  149.      */
  150.     public void repaint() {
  151.     parent.repaint(0, X, Y, W, H);
  152.     }
  153.  
  154.     /**
  155.      * This method tests to see if a point is inside this ImageArea.
  156.      * The standard method assumes a rectangular area as parsed by
  157.      * the standard initializer.  If a more complex area is required
  158.      * then this method will have to be overridden by the subclass.
  159.      */
  160.     public boolean inside(int x, int y) {
  161.     return (x >= X && x < (X + W) && y >= Y && y < (Y + H));
  162.     }
  163.  
  164.     /**
  165.      * This utility method draws a rectangular subset of a highlight
  166.      * image.
  167.      */
  168.     public void drawImage(Graphics g, Image img, int imgx, int imgy,
  169.               int x, int y, int w, int h) {
  170.     Graphics ng = g.create();
  171.     ng.clipRect(x, y, w, h);
  172.     ng.drawImage(img, imgx, imgy, this);
  173.     }
  174.  
  175.     /**
  176.      * This method handles the updates from drawing the images.
  177.      */
  178.     public boolean imageUpdate(Image img, int infoflags,
  179.                    int x, int y, int width, int height) {
  180.     if (img == hlImage) {
  181.         return parent.imageUpdate(img, infoflags, x + X, y + Y,
  182.                       width, height);
  183.     } else {
  184.         return (infoflags & (ALLBITS | ERROR)) == 0;
  185.     }
  186.     }
  187.  
  188.     /**
  189.      * This utility method records a string to be shown in the status bar.
  190.      */
  191.     public void showStatus(String msg) {
  192.     status = msg;
  193.     parent.newStatus();
  194.     }
  195.  
  196.     /**
  197.      * This utility method returns the status string this area wants to
  198.      * put into the status bar.  If no previous area (higher in the
  199.      * stacking order) has yet returned a status message, prevmsg will
  200.      * be null and this area will then return its own message, otherwise
  201.      * it will leave the present message alone.
  202.      */
  203.     public String getStatus(String prevmsg) {
  204.     return (prevmsg == null) ? status : prevmsg;
  205.     }
  206.  
  207.     /**
  208.      * This utility method tells the browser to visit a URL.
  209.      */
  210.     public void showDocument(URL u) {
  211.     parent.getAppletContext().showDocument(u);
  212.     }
  213.  
  214.     /**
  215.      * This method highlights the specified area when the user enters
  216.      * it with his mouse.  The standard highlight method is to replace
  217.      * the indicated rectangular area of the image with the primary
  218.      * highlighted image.
  219.      */
  220.     public void highlight(Graphics g) {
  221.     }
  222.  
  223.     /**
  224.      * The checkEnter method is called when the mouse is inside the
  225.      * region to see if the area needs to have its enter method called.
  226.      * The default implementation simply checks if the entered flag is
  227.      * set and only calls enter if it is false.
  228.      */
  229.     public boolean checkEnter(int x, int y) {
  230.     if (!entered) {
  231.         entered = true;
  232.         enter(x, y);
  233.     }
  234.     return isTerminal();
  235.     }
  236.  
  237.     /**
  238.      * The checkExit method is called when the mouse is outside the
  239.      * region to see if the area needs to have its exit method called.
  240.      * The default implementation simply checks if the entered flag is
  241.      * set and only calls exit if it is true.
  242.      */
  243.     public void checkExit() {
  244.     if (entered) {
  245.         entered = false;
  246.         exit();
  247.     }
  248.     }
  249.  
  250.     /**
  251.      * The isTerminal method controls whether events propagate to the
  252.      * areas which lie beneath this one.
  253.      * @return true if the events should be propagated to the underlying
  254.      * areas.
  255.      */
  256.     public boolean isTerminal() {
  257.     return false;
  258.     }
  259.  
  260.     /**
  261.      * The enter method is called when the mouse enters the region.
  262.      * The location is supplied, but the standard implementation is
  263.      * to call the overloaded method with no arguments.
  264.      */
  265.     public void enter(int x, int y) {
  266.     enter();
  267.     }
  268.  
  269.     /**
  270.      * The overloaded enter method is called when the mouse enters
  271.      * the region.  This method can be overridden if the ImageArea
  272.      * does not need to know where the mouse entered.
  273.      */
  274.     public void enter() {
  275.     }
  276.  
  277.     /**
  278.      * The exit method is called when the mouse leaves the region.
  279.      */
  280.     public void exit() {
  281.     }
  282.  
  283.     /**
  284.      * The press method is called when the user presses the mouse
  285.      * button inside the ImageArea.  The location is supplied, but
  286.      * the standard implementation is to call the overloaded method
  287.      * with no arguments.
  288.      * @return true if this ImageMapArea wants to prevent any underlying
  289.      * areas from seeing the press
  290.      */
  291.     public boolean press(int x, int y) {
  292.     return press();
  293.     }
  294.  
  295.     /**
  296.      * The overloaded press method is called when the user presses the
  297.      * mouse button inside the ImageArea.  This method can be overridden
  298.      * if the ImageArea does not need to know the location of the press.
  299.      * @return true if this ImageMapArea wants to prevent any underlying
  300.      * areas from seeing the press
  301.      */
  302.     public boolean press() {
  303.     return isTerminal();
  304.     }
  305.  
  306.     /**
  307.      * The lift method is called when the user releases the mouse button.
  308.      * The location is supplied, but the standard implementation is to
  309.      * call the overloaded method with no arguments.  Only those ImageAreas
  310.      * that were informed of a press will be informed of the corresponding
  311.      * release.
  312.      * @return true if this ImageMapArea wants to prevent any underlying
  313.      * areas from seeing the lift
  314.      */
  315.     public boolean lift(int x, int y) {
  316.     return lift();
  317.     }
  318.  
  319.     /**
  320.      * The overloaded lift method is called when the user releases the
  321.      * mouse button.  This method can be overridden if the ImageArea
  322.      * does not need to know the location of the release.
  323.      * @return true if this ImageMapArea wants to prevent any underlying
  324.      * areas from seeing the lift
  325.      */
  326.     public boolean lift() {
  327.     return isTerminal();
  328.     }
  329.  
  330.     /**
  331.      * The drag method is called when the user moves the mouse while
  332.      * the button is pressed.  Only those ImageAreas that were informed
  333.      * of a press will be informed of the corresponding mouse movements.
  334.      * @return true if this ImageMapArea wants to prevent any underlying
  335.      * areas from seeing the drag
  336.      */
  337.     public boolean drag(int x, int y) {
  338.     return isTerminal();
  339.     }
  340.  
  341.   public String getAppletInfo() {
  342.     return "Title: ImageArea \nAuthor: Jim Graham \nThis class performs the basic functions that most ImageArea classes will need and delegates specific actions to the subclasses.";
  343.   }
  344. }
  345.  
  346.